package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 中国经济网解析
 * @author Persh
 *
 */
public class CecnNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
	"http://.*.ce.cn/.*/[0-9]{6}/[0-9]{2}/[^.]*.shtml",
	"http://.*.ce.cn/.*/[0-9]{4}[-][0-9]{2}[-][0-9]{2}/[0-9]*.html",
	"http://.*.ce.cn/.*/[0-9]{4}/[0-9]*/[0-9]*.html",
	"http://.*.ce.cn/.*/[0-9]{4}/[^.]*/[0-9]*.html",
	"http://.*.ce.cn/.*/[0-9]{8}/[0-9]*.html",
	"http://.*.ce.cn/.*/.*/[0-9]*/[0-9]*/t[0-9]*_[0-9]*.shtml"};

	private int index=0;

	
	public boolean isDetailPage(String url) {
		//德语，俄语或其他语种不抓取
		if(url.startsWith("http://de.ce.cn/") || url.startsWith("http://ru.ce.cn/") || url.startsWith("http://ar.ce.cn/") || url.startsWith("http://ja.ce.cn/")
				|| url.startsWith("http://fr.ce.cn/") || url.startsWith("http://es.ce.cn/"))
			return false;
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				index=i;
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
			title =doc.select("h1#articleTitle").text();
			if(title==null || title.trim().length()==0){
				title =doc.select("div.T_Title").text();
				if(title==null || title.trim().length()==0){
					title =doc.select("div.main>h1").text();
					if(title==null || title.trim().length()==0){
						title =doc.select("ul.detaila").text();
						if(title==null || title.trim().length()==0){
							title =doc.select("div.main-left-2>h1").text();
							if(title==null || title.trim().length()==0){
								title =doc.select("td#articleTitle>font>strong>font").text();
								if(title==null || title.trim().length()==0){
									title=doc.select("title").text();
									title=StringUtils.substringBefore(title, "_");
									if(title==null || title.trim().length()==0){
									}
								}
							}
						}
					}
				}
			}
			content=HtmlCleaner.getContentHtml(url ,doc.select("div#articleText"));
			if(content==null || content.trim().length()==0){
				content=HtmlCleaner.getContentHtml(url ,doc.select("div#contents"));
			}
			if(content==null || content.trim().length()==0){
				content=HtmlCleaner.getContentHtml(url ,doc.select("div#contentText"));
			}
			if(content==null || content.trim().length()==0){
				content=HtmlCleaner.getContentHtml(url ,doc.select("div.detailc"));
			}
			if(content==null || content.trim().length()==0){
				content=HtmlCleaner.getContentHtml(url ,doc.select("div.main-left-2>div.article>p"));
			}
			if(content==null || content.trim().length()==0){
				content=HtmlCleaner.getContentHtml(url ,doc.select("td#articleText"));
			}
			if(content==null || content.trim().length()==0){
				content=HtmlCleaner.getContentHtml(url ,doc.select("div.TRS_Editor"));
			}
			content=content.replace("正在加载中... ", "");

			date=DateUtils.matchDate(doc.select("span#articleTime").text());
			if(date ==null){
				date=DateUtils.matchDate(doc.select("div.T_Ly_Time").text());
			}
			if(date==null){
				date=DateUtils.matchDate(doc.select("div.info>span").text());
			}
			if(date==null){
				date=DateUtils.matchDate(doc.select("ul.detailb").text());
			}
			if(date==null){
				date=DateUtils.matchDate(doc.select("div.main-left-2>div.laiyuan>span").text());
			}
			if(date==null){
				date=DateUtils.matchDate(doc.select("center").text());
			}

		if (date==null){
			date=JSoupUtils.matchDate(doc, "来源");
		}
		String author= JSoupUtils.matchAuthor(doc, "来源:");
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);
		news.setAuthor(author);
		return news;
	}

	public static void main(String[] args) {
		CecnNewsAnalyse analyse=new CecnNewsAnalyse();
		String url="http://district.ce.cn/newarea/roll/201611/07/t20161107_17573055.shtml";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
